package com.huawei.struts.util;

import java.util.List;

public class PageBean {
	private List list; // 要返回的某一页的记录列表
	private  int allRow;// 总记录数
	private  int totalPage; // 总页数
	private  int currentPage=1;// 当前页
	private  int pageSize=10; // 每页记录数
	private int firstResult;
	private int upPage;//上一页
	private int downPage;//下一页
	private boolean isFirstPage; // 是否为第一页
	private boolean isLastPage;// 是否为最后一页
	private boolean hasPreviousPage; // 是否有前一页
	private boolean hasNextPage; // 是否有下一页
	
	

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getUpPage() {
		return upPage;
	}

	public void setUpPage(int upPage) {
		this.upPage = upPage;
	}

	public int getDownPage() {
		return downPage;
	}

	public void setDownPage(int downPage) {
		this.downPage = downPage;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getAllRow() {
		return allRow;
	}

	public void setAllRow(int allRow) {
		this.allRow = allRow;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public static int countTotalPage(final int pageSize, final int allRow) {
		int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow
				/ pageSize + 1;
		return totalPage;
	}

	public  PageBean pagationTemplate(int allRows,int currentPageIndex){
		PageBean pageBean=new PageBean();
		int totalPage=countTotalPage(pageSize,allRows);
		int correctCurrentPage=CorrectCurrentPage(currentPageIndex,totalPage);
		int firstResult=countOffset(pageSize,correctCurrentPage,totalPage);
		pageBean.setFirstResult(firstResult);
		pageBean.setCurrentPage(correctCurrentPage);
		pageBean.setAllRow(allRows);
		pageBean.setTotalPage(totalPage);
		pageBean.setPageSize(pageSize);
		return pageBean;
	}
	public static int countOffset(final int pageSize, final int currentPage,final int totalPage) {
		int curpage=currentPage;
		if(curpage>totalPage)
		{
			curpage=totalPage;
		}
		else if(currentPage<=1)
		{
			curpage=1;
		}
		final int offset = pageSize * (curpage - 1);
		return offset;
	}

	public static int CorrectCurrentPage(final int currentPage,final int totalPage) {
		int curPage;
		if(currentPage<=1)
		{
			curPage=1;
		}
		else if(currentPage>totalPage)
		{
			curPage=totalPage;
		}
		else
		{
			curPage=currentPage;
		}
		return curPage;
	}
	
	
	

}
